<template>
  <div class="main-container">
    <div class="main-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{path: '/course/classify'}">课程分类管理</el-breadcrumb-item>
        <el-breadcrumb-item>新增课程分类</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="wrap">
      <div class="content">
        <el-form class="mt-30" ref="form" :model="form" label-width="150px" style="width: 450px;">
          <el-form-item label="分类名称" prop="courseName" :rules="[ { required: true, message: '分类名称不能为空'}]">
            <el-input v-model="form.courseName"></el-input>
          </el-form-item>
          <el-form-item label="是否一级分类">
            <el-switch v-model="form.courseLevel" :active-value="1" :inactive-value="2"></el-switch>
          </el-form-item>
          <el-form-item label="上级分类" v-if="form.courseLevel == 2">
            <el-select v-model="form.parentCode" placeholder="选择上级分类" style="width: 100%">
              <el-option v-for="(item,index) in courseList" :key="index" :label="item.courseName" :value="item.courseCode"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" prop="sort">
              <div class="help-tip el-iconhelp_tip element-icons" style="left: -62px;">
                <p class="help-tip-content">页面中可以通过“排序”进行升序排序。</p>
              </div>
              <el-input-number v-model="form.sort" :min="0" :controls="false"></el-input-number>
            </el-form-item>
          <el-form-item label="标题" prop="seoTitle">
            <el-input v-model="form.seoTitle" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="描述" prop="seoDesc">
            <el-input v-model="form.seoDesc" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="关键字" prop="seoKeyword">
            <el-input v-model="form.seoKeyword" type="textarea"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">保存</el-button>
            <el-button @click="onReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        form: {
          courseName: ''
        },
        courseList: []
      }
    },
    activated() {
      this.$refs.form.resetFields()
      this.loadCourseList()
    },
    created() {
      this.loadCourseList()
    },
    methods: {
      loadCourseList() {
        this.$api.courseType.getList({courseLevel:1, documentType:1}).then(res => {
          if(res.success){
            this.courseList = res.data
          }
        })
      },
      onSubmit() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            // if(this.form.courseName == '全域营销概览'){
            //   this.$alert('名称不能为"全域营销概览"')
            //   return false;
            // }
            const params = {
              courseName: this.form.courseName,
              courseLevel: this.form.courseLevel,
              seoTitle: this.form.seoTitle,
              seoDesc: this.form.seoDesc,
              seoKeyword: this.form.seoKeyword,
              parentCode: this.form.courseLevel == 2 ? this.form.parentCode : '',
              documentType: 1,
              sort: this.form.sort,
              remark: this.form.remark
            }
            this.$api.courseType.add(params).then(res => {
              if(res.success) {
                this.$message.success('保存成功')
                this.$refs.form.resetFields()
                this.$router.push('/course/classify');
              }
            })
          }
        })
      },
      onReset() {
        this.$refs.form.resetFields()
      }
    }
  }
</script>

<style>

 
</style>
